Is there a way to get the PREMATCH ($`) and POSTMATCH ($') from pcrecpp?

Posted by Eric Peers on Stack Overflow See other posts from Stack Overflow or by Eric Peers
Published on 2010-03-14T17:27:54Z Indexed on 2010/03/14 17:35 UTC
Read the original article Hit count: 236

Filed under:
|
|
|

Is there a way to obtain the C++ equivalent of Perl's PREMATCH ($`) and POSTMATCH ($') from pcrecpp? I would be happy with a string, a char *, or pairs indices/startpos+length that point at this.

StringPiece seems like it might accomplish part of this, but I'm not certain how to get it.

in perl:

$_ = "Hello world";
if (/lo\s/) {
    $pre = $`; #should be "Hel"
    $post = $'; #should be "world"
}

in C++ I would have something like:

string mystr = "Hello world"; //do I need to map this in a StringPiece?
if (pcrecpp::RE("lo\s").PartialMatch(mystr)) { //should I use Consume or FindAndConsume?
   //What should I do here to get pre+post matches???
}

pcre plainjane c seems to have the ability to return the vector with the matches including the "end" portion of the string, so I could theoretically extract such a pre/post variable, but that seems like a lot of work. I like the simplicty of the pcrecpp interface.

Suggestions? Thanks!

--Eric

© Stack Overflow or respective owner

Related posts about pcre

Related posts about c++